﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Finance_wasteBookAdd : ATPages
{
    ZSql zsql = new ZSql();
    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string departmentid="0",userid="0"; 
            hidModuid.Value = Request.QueryString["moduid"].ToString();
            lblPath.Text = f.getModuleidAdd(hidModuid.Value);
            EditDate.Text = DateTime.Now.ToString();
            EditUserName.Text = Server.UrlDecode(Request.Cookies["user"].Values["realname"]);
            if(!string.IsNullOrEmpty(Request.QueryString["id"]))//
            {
                ZSql zs = new ZSql();
                zs.Open("select * from CS_wasteBook where wasteBookId=" + Request.QueryString["id"]);
                    departmentId.SelectedValue = zs.m_table.Rows[0]["departmentId"].ToString();
                    departmentid = zs.m_table.Rows[0]["departmentId"].ToString();
                    userid = zs.m_table.Rows[0]["userId"].ToString();
                    financeType.SelectedValue = zs.m_table.Rows[0]["financeType"].ToString();
                    financeMoney.Text = zs.m_table.Rows[0]["financeMoney"].ToString();
                    PaymentMethods.Text = zs.m_table.Rows[0]["PaymentMethods"].ToString();
                    financeCode.Text = zs.m_table.Rows[0]["financeCode"].ToString();
                    Remark.Text = zs.m_table.Rows[0]["Remark"].ToString();
                    if (zs.m_table.Rows[0]["AuditState"].ToString() == "1")
                        AuditState.Checked = true;
                    else
                        AuditState1.Checked = true;
                    EditUserName.Text = zs.m_table.Rows[0]["EditUserName"].ToString();
                    EditDate.Text = zs.m_table.Rows[0]["EditDate"].ToString();
                zs.Close();
            }
            getDepartment();
            getUsers(departmentid, userid);
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        ZSql info = new ZSql();
        string action = Request.QueryString["action"];
        string sql = "";
        if (action == "add")//新增
            sql = "select top 1 * from CS_wasteBook";
        else//修改
            sql = "select top 1 * from CS_wasteBook where wasteBookId=" + Request.QueryString["id"];

        info.Open(sql);
        info.NextRow();
        if (action == "add")//新增
        {
            info.NewRow();
        }
        info["departmentId"] = Request.Form["departmentId"].ToString();
        info["userId"] = Request.Form["userId"].ToString();
        info["financeType"] = Request.Form["financeType"].ToString();
        info["financeMoney"] = Request.Form["financeMoney"].ToString();
        info["PaymentMethods"] = Request.Form["PaymentMethods"].ToString();
        info["financeCode"] = Request.Form["financeCode"].ToString();
        info["Remark"] = Request.Form["Remark"].ToString();
    

        info["AuditState"] = Request.Form["AuditState"];
        info["EditUserName"] = Server.UrlDecode(Request.Cookies["user"].Values["realname"]);//编辑人
        info["EditDate"] = EditDate.Text.ToString();//时间

        info.Update();
        info.Close();
        Response.Write("<script>alert('操作已成功，请返回！');</script>");//返回页面
        Response.Write("<script>window.location='wasteBook.aspx?moduid=" + hidModuid.Value + "';</script>");//返回页面

    }

    protected void btnSelec_Click(object sender, EventArgs e)
    {
        //zsql.Open("select * from Brand where Brand like'%" + txtselecName.Text + "%'");
        //GvData1.DataSource = zsql.m_table;
        //GvData1.DataBind();
        //flag();
    }

    private void getDepartment()
    {
        ZSql Dep = new ZSql();
        Dep.Open("select * from Department order by Deptid");
        departmentId.DataSource = Dep.m_table;
        departmentId.DataTextField = "Deptname";
        departmentId.DataValueField = "Deptid";
        departmentId.DataBind();
        departmentId.Items.Insert(0, new ListItem("选择所属部门", ""));
        Dep.Close();

    }

    private void getUsers(string departmentid, string userid)//动态生成部门,人员JS代码
    {
        ZSql rs = new ZSql();
        ZSql zs = new ZSql();
        string JsOptions = "";
        JsOptions += "<script language='javascript'>\n";
        JsOptions += "var select1_len = document.all('departmentId').options.length;\n";
        JsOptions += "var select2 = new Array(select1_len); \n";
        JsOptions += "for (i=0; i<select1_len; i++)\n";
        JsOptions += "{\n";
        JsOptions += "  select2[i] = new Array();\n";
        JsOptions += "}\n";
        //动态生成数组
        JsOptions += "select2[0][0] = new Option('选择人员', '');  \n";
        zs.Open("select * from Department order by Deptid");
        for (int i = 0; i < zs.m_table.Rows.Count; i++)
        {
            JsOptions += "select2[" + (i + 1).ToString() + "][0] = new Option('选择人员', '');  \n";
            rs.Open("select  * from CS_Staff where deptId=" + zs.m_table.Rows[i]["Deptid"].ToString() + " order by deptId");
            for (int j = 0; j < rs.m_table.Rows.Count; j++)
            {
                JsOptions += "select2[" + (i+1).ToString() + "][" + (j+1).ToString() + "] = new Option('" + rs.m_table.Rows[j]["StaffName"].ToString() +"', '" + rs.m_table.Rows[j]["StaffId"].ToString() +"');  \n";
            }
            rs.Close();
            JsOptions += "\n";
        }
        zs.Close();


        JsOptions += "function redirec(x) \n";
        JsOptions += "{ \n";
        JsOptions += "  var temp = document.all('userId'); \n";
        JsOptions += "  temp.options.length = 0;\n";//清除所有
        JsOptions += "  for (i=0;i<select2[x].length;i++) \n";
        JsOptions += "  { \n";
        JsOptions += "      temp.options[i]=new Option(select2[x][i].text,select2[x][i].value); \n"; 
        JsOptions += "  } \n";
        JsOptions += "  temp.options[0].selected = true; \n";
        JsOptions += "} \n\n";

        JsOptions += "function getUser(departmentId,userid) \n";
        JsOptions += "{ \n";
        JsOptions += "  var department = document.all('departmentId'); \n";
        JsOptions += "  var temp = document.all('userId'); \n";
        JsOptions += "  for(i=0;i<department.options.length;i++) \n";
        JsOptions += "  { \n";
        JsOptions += "      if(department.options[i].value==departmentId)redirec(i); \n";
        JsOptions += "  } \n\n";
        JsOptions += "  for (i=0;i<temp.options.length;i++) \n";
        JsOptions += "  { \n";
        JsOptions += "      if(temp.options[i].value==userid)temp.options[i].selected = true; \n";
        JsOptions += "  } \n";
        JsOptions += "} \n";
        JsOptions += "getUser(" + departmentid + "," + userid + ") \n";
        JsOptions += "</script>\n";

        lbljsOptions.Text = JsOptions;

        
    }
}
